//
//  DataHander.m
//
//  Created by chen on 14/11/19.
//  Copyright © 2014年 chen. All rights reserved.
//

#import "DataHander.h"
#import <sqlite3.h>
#import "CQHotModel.h"
@implementation DataHander
/** 创建一个sqlite3对象(结构体) */
sqlite3 *db;
#pragma mark - 知识点 创建单例
+ (instancetype)shareDataHander {
    //静态区创建对象,只初始化一次
    static DataHander *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[DataHander alloc] init];
    }
    return dataBase;
}
#pragma mark - 打开数据库
/** 核心APIsqlite3_open */
- (void)open {
    if (db != nil) {
        DLog(@"数据库已经开启");
        return;
    }
    NSString *pathOfCaches = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"HotGift.sqlite"];
    //参数1:是c字符串 参数2:是**,所以传人的参数是地址.
    int result = sqlite3_open(pathOfCaches.UTF8String, &db);
    //根据函数返回值判断执行是否正确
    if (result == SQLITE_OK) {
        DLog(@"数据库打开成功");
        
    } else {
        DLog(@"数据库打开失败,错误码%d", result);
    }
    
}
#pragma mark - 创建表格
- (void)creatTable {
    NSString *creatTableSQL = @"create table if not exists HotGift(id integer primary key autoincrement, favorites_count integer, name text, cover_image_url text, price text, url text)";
    int result = sqlite3_exec(db, creatTableSQL.UTF8String, nil, nil, nil);
    
    if (result == SQLITE_OK) {
        DLog(@"创建表格成功");
    } else {
        DLog(@"创建表格失败");
    }
}
#pragma mark - 插入数据
- (void)insertHotModel:(CQHotModel *)hotModel {
    /** 插入数据的SQL语句 */
    NSString *insertHotGiftSQL =[NSString stringWithFormat:@"insert into HotGift(favorites_count, name, cover_image_url, price, url) values('%ld','%@','%@','%@','%@')", (long)hotModel.favorites_count,hotModel.name, hotModel.cover_image_url, hotModel.price, hotModel.url];
    int result = sqlite3_exec(db, insertHotGiftSQL.UTF8String, nil, nil, nil);
    if (result == SQLITE_OK) {
        DLog(@"插入数据成功");
    } else {
        DLog(@"插入数据失败 %d", result);
    }
}
#pragma mark - 删除数据
- (void)deleteHotModel:(CQHotModel *)model {
    NSString *deleteHotGiftSQL =[NSString stringWithFormat:@"delete from HotGift where name = '%@'", model.name];
    int result = sqlite3_exec(db, deleteHotGiftSQL.UTF8String, nil, nil, nil);
    if (result == SQLITE_OK) {
        DLog(@"删除数据成功");
    } else {
        DLog(@"删除数据失败 %d", result);
    }
}


#pragma mark - 检索数据
/** API:sqlite3_stmt */
- (NSArray<CQHotModel *> *)selectHotModel{
    NSMutableArray *arr = [NSMutableArray array];
    NSString *selectSQL = [NSString stringWithFormat:@"select *from HotGift"];
    //声明一个stmt对象
    sqlite3_stmt *stmt;
    int result =  sqlite3_prepare_v2(db, selectSQL.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        //每行都执行查询语句
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int favorites_count = sqlite3_column_int(stmt, 1);
            const unsigned char *name = sqlite3_column_text(stmt, 2);
            const unsigned char *cover_image_url = sqlite3_column_text(stmt, 3);
            const unsigned char *price = sqlite3_column_text(stmt, 4);
            const unsigned char *url = sqlite3_column_text(stmt, 5);
        //创建model,赋值后放入数组
            CQHotModel *model = [[CQHotModel alloc] init];
            model.favorites_count = favorites_count;
            model.name = [NSString stringWithUTF8String:(const char *)name];
            model.cover_image_url = [NSString stringWithUTF8String:(const char *)cover_image_url];
            model.price = [NSString stringWithUTF8String:(const char *)price];
            model.url = [NSString stringWithUTF8String:(const char *)url];
            [arr addObject:model];
            
        }
        
    } else {
        DLog(@"未能启动正常查询,错误:%d", result);
    }
    //释放对象
    sqlite3_finalize(stmt);
    return arr;
}
#pragma mark - closeTable
- (void)closeTable {
    int result = sqlite3_close(db);
    if (result == SQLITE_OK) {
        DLog(@"关闭表格成功");
    } else {
        DLog(@"关闭表格失败%d", result);
    }
}
@end
